<?php

namespace App\Admin\Validate\Base;

use App\Common\Enums\Base\SuperConfigEnumType;
use App\Common\Exception\ApiMessageException;
use App\Common\Model\Base\SuperConfig;
use App\Common\Validate\AbstractValidate;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

/**
 * 超级配置 表单验证定义
 * 方法对应控制器的方法
 */
class SuperConfigValidate extends AbstractValidate
{
    protected function allRules(): array
    {
        return [
            'id'                   => ['require'],
            'sign|配置的签名'      => ["require", 'unique' => implode(',', [SuperConfig::class, 'sign', $this->input('id')]), 'regex' => '/^[a-zA-Z]\w*/'],
            'name|配置的名称'      => ["require"],
            'type|配置的类型'      => ["require", 'in' => SuperConfigEnumType::values()],
            'config_info|配置详情' => function () {
                if (in_array($this->input('type'), [1, 4]) && !$this->input('config_info')) {
                    return "配置详情必须";
                }

                if (!is_array($this->input('config_info'))) {
                    return "配置详情必须为数组";
                }
                if (count(array_unique(array_map('strtolower', array_column($this->input('config_info'), 'field')))) < count($this->input('config_info'))) {
                    return "配置选项的key值不能重复";
                }

                foreach ($this->input('config_info') as $item) {
                    if (!preg_match('/^[a-zA-Z]/', $item['field'])) {
                        return "配置选项的key值必须以字母开头";
                    }
                    if (empty($item['formType'])) {
                        return "配置选项的表单类型不能为空";
                    }
                }


                return true;
            },
            'config_value|配置值'  => ["require", "array"]
        ];
    }

    public function attributes(): array
    {
        return [
            'id'                     => 'ID',
            'sign'                   => '配置的签名',
            'name'                   => '配置的名称',
            'type'                   => '配置的类型',
            'config_info'            => '配置详情',
            'config_value'           => '配置值',
        ];
    }

    /**
     * @return SuperConfigValidate
     */
    public function sceneUpdate(): SuperConfigValidate
    {
        return $this->only(["id", "sign", "name", "type", "config_info"]);
    }

    /**
     * @return SuperConfigValidate
     */
    public function sceneStore(): SuperConfigValidate
    {
        return $this->only(["sign", "name", "type", "config_info"]);
    }

    /**
     * @return SuperConfigValidate
     */
    public function sceneDestroy(): SuperConfigValidate
    {
        return $this->rule(['ids|要删除的数据' => 'require|array'])->only(['ids']);
    }

    /**
     * @return SuperConfigValidate
     * @throws ApiMessageException
     */
    public function updateBaseConfigScene(): SuperConfigValidate
    {
        if (!$this->post('id')) {
            throw new ApiMessageException('无法确认配置');
        }

        return $this->only([]);
    }

    protected function allMessages(): array
    {
        return [];
    }
}
